# -*- codeing = utf-8 -*-
# @Time : 2022/8/5 22:21
# @Author : ZN
# @Email : 747723508@qq.com
# @File : serializers.py
# @Software: PyCharm
from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer

from users.serializers import UserSerializer
from .models import *
#
# class Simple(ModelSerializer):
#     class Meta:
#         model = Classes
#         fields = ['creat_time','update_time','name','Slogan']


class ClassesSerializer(ModelSerializer):
    memberDetail = SerializerMethodField()

    class Meta:
        model = Classes
        exclude = ['is_delete']

    def get_memberDetail(self,instance):
        members = UserSerializer(instance.member.all(),many=True).data
        return {
            'teachers':[i['username'] for i in members if i['is_superuser'] or 1 in i['groups']],
            'students':[i['username']for i in members if 2 in i['groups']],
        }

class CreateUserSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['username','email','first_name']
        extra_kwargs = {
            'username':{'required':True},
            'email':{'required':True}
        }